package itstudio.instructor.ui;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.http.Header;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import itstudio.app.wq.R;
import itstudio.instructor.config.Config;
import itstudio.instructor.http.TwitterRestClient;
import itstudio.instructor.util.ToastUtil;
import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class ReviseMail extends Activity{
	
	private View back_layout;
	private EditText email_et;
	private EditText password_et;
	private Button save_btn;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_revise_mail);
		back_layout=findViewById(R.id.revise_mail_back_layout);
		email_et=(EditText) findViewById(R.id.email_et);
		password_et=(EditText) findViewById(R.id.password_et);
		save_btn=(Button) findViewById(R.id.save_btn);
		
		back_layout.setOnClickListener(onclick);
		save_btn.setOnClickListener(onclick);

	}
	// 邮箱正则表达式
    public boolean isEmail(String email){     
        String str="^([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)*@([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)+[\\.][A-Za-z]{2,3}([\\.][A-Za-z]{2})?$";
           Pattern p = Pattern.compile(str);     
           Matcher m = p.matcher(email);     
           return m.matches();    
      } 
    OnClickListener onclick = new   OnClickListener() {

		@Override
		public void onClick(View view) {
			// TODO Auto-generated method stub
			switch (view.getId()) {
			case R.id.revise_mail_back_layout:
				finish();
				break;
			case R.id.save_btn:
				saveInfo();
				break;
	
			default:
				break;
			}
		}
	};
	private void saveInfo() {
		// TODO Auto-generated method stub

		if(TextUtils.isEmpty(email_et.getText()) || email_et.getText().toString().length()>35){
			ToastUtil.showErrorMsg(ReviseMail.this, R.string.error_email_null);
			return;
		}
		if(!isEmail(email_et.getText().toString())){
			ToastUtil.showErrorMsg(ReviseMail.this, R.string.error_email_format);
			return;
		}
		if(TextUtils.isEmpty(password_et.getText())||password_et.getText().length()<6 || password_et.getText().length()>12 ){
			ToastUtil.showErrorMsg(ReviseMail.this, R.string.error_password_format);
			return;
		}
		httpSaveInfo(password_et.getText().toString(),email_et.getText().toString());
	}
	private void httpSaveInfo(String password,String email){
		
		save_btn.setText(R.string.saving);
		RequestParams params = new RequestParams();
		params.put("user.password",password);
		params.put("user.email", email);
		TwitterRestClient.post(Config.AC_USER_EMAIL, params, new AsyncHttpResponseHandler(){

			@Override
			public void onFailure(int arg0, Header[] arg1, byte[] arg2,
					Throwable arg3) {
				// TODO Auto-generated method stub
				save_btn.setText(R.string.save);
				ToastUtil.showErrorMsg(ReviseMail.this, R.string.fail_email_save);
			}

			@Override
			public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
				//加载成功
				save_btn.setText(R.string.save);
				String result = new String (arg2);
				if(result.indexOf(Config.SUCCESS)!=-1){
					ToastUtil.showSuccessMsg(ReviseMail.this, R.string.success_eamil_save);
				}
				else if(result.indexOf("exist")!=-1){
					ToastUtil.showErrorMsg(ReviseMail.this, R.string.fail_eamil_exist);
				}
				else{
					ToastUtil.showErrorMsg(ReviseMail.this, R.string.fail_email_save);
				}
		}});
	}
}
